import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';

class DrawLots extends StatefulWidget {
  const DrawLots({super.key});

  @override
  State<DrawLots> createState() => _WebDetailState();
}

class _WebDetailState extends State<DrawLots> {
  final uuid = Get.parameters['uuid'];
  final draw = Get.parameters['draw'];
  // ignore: non_constant_identifier_names
  final is_box = Get.parameters['is_box'];

  final RxMap productsData = {}.obs;
  final RxList boxInfo = [].obs;
  final RxInt _index = 0.obs;
  final RxMap indexItem = {}.obs;
  final RxMap but = {}.obs;
  final RxMap drawData = {}.obs;
  RxList timeList = [].obs;
  RxList drawList = [].obs;

  RxInt timeIndex = 0.obs;

  upraw() {
    Get.find<HTTPS>().postData(
        '/getDraw', {'product_id': "${productsData['id']}"}).then((res) {
      if (res != null) {
        Get.dialog(
          AlertDialog(
              backgroundColor: Colors.transparent,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5)),
              content: Stack(
                children: [
                  Image.asset(
                    'static/tan.png',
                    width: 255,
                    fit: BoxFit.fitWidth,
                  ),
                  Positioned(
                      top: 50,
                      right: 0,
                      left: 0,
                      child: Column(
                        children: [
                          Text(
                            '恭喜获得抽签码',
                            style: TextStyle(
                                fontSize: 24, color: Color(0XFF0F172B)),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Text(
                            '此次抽签结果',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B)),
                          ),
                          Text(
                            '将于${res['draw']['draw_date']}公布',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B)),
                          ),
                          SizedBox(
                            height: 20,
                          ),
                          Container(
                            height: 65,
                            width: 212,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8),
                                color: Color(0XFFF8F9FD)),
                            child: Text(
                              '${res['draw']['draw_list'][res['draw']['draw_list'].length - 1]['code']}',
                              style: TextStyle(
                                  fontSize: 30,
                                  color: Color(0XFF1F0588),
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                          GestureDetector(
                            onTap: () {
                              Get.back();
                            },
                            child: Container(
                              margin: EdgeInsets.only(top: 20),
                              width: 210,
                              height: 50,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(25),
                                  color: Color(0XFF0F172B)),
                              child: Text('确定',
                                  style: TextStyle(
                                      fontSize: 16, color: Color(0XFFFFFFFF))),
                            ),
                          )
                        ],
                      )),
                ],
              )),
        );
      }
    });
  }

  getCodes() {
    Get.dialog(
      AlertDialog(
          backgroundColor: Colors.transparent,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
          content: Stack(
            children: [
              Image.asset(
                'static/tan.png',
                width: 250,
                fit: BoxFit.fitWidth,
              ),
              Positioned(
                  top: 40,
                  right: 0,
                  left: 0,
                  child: Column(
                    children: [
                      Text(
                        '我的抽签码',
                        style:
                            TextStyle(fontSize: 24, color: Color(0XFF0F172B)),
                      ),
                      Container(
                          width: 200,
                          height: 170,
                          child: ListView(
                            children: [
                              for (var item in drawList)
                                Container(
                                  margin: EdgeInsets.symmetric(vertical: 5),
                                  height: 35,
                                  padding: EdgeInsets.only(left: 15),
                                  alignment: Alignment.centerLeft,
                                  decoration: BoxDecoration(
                                    image: DecorationImage(
                                      image: AssetImage(item['status'] == 1
                                          ? 'static/web/zhongma.png'
                                          : 'static/web/chouma.png'),
                                      fit: BoxFit.fitWidth,
                                    ),
                                  ),
                                  child: Text(
                                    '${item['code']}',
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Color(0XFF0F172B),
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),

                              // Container(
                              //   height: 35,
                              //   margin: EdgeInsets
                              //       .symmetric(
                              //           vertical:
                              //               5),
                              //   padding: EdgeInsets
                              //       .only(
                              //           left: 15),
                              //   alignment: Alignment
                              //       .centerLeft,
                              //   decoration:
                              //       BoxDecoration(
                              //     image:
                              //         DecorationImage(
                              //       image: AssetImage(
                              //           'static/web/zhongma.png'),
                              //       fit: BoxFit
                              //           .fitWidth,
                              //     ),
                              //   ),
                              //   child: Text(
                              //     'FS3A34',
                              //     style: TextStyle(
                              //         fontSize:
                              //             16,
                              //         color: Color(
                              //             0XFF0F172B),
                              //         fontWeight:
                              //             FontWeight
                              //                 .bold),
                              //   ),
                              // ),
                            ],
                          )),
                      GestureDetector(
                        onTap: () {
                          Get.back();
                        },
                        child: Container(
                          margin: EdgeInsets.only(top: 15),
                          width: 210,
                          height: 50,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(25),
                              color: Color(0XFF0F172B)),
                          child: Text('确定',
                              style: TextStyle(
                                  fontSize: 16, color: Color(0XFFFFFFFF))),
                        ),
                      )
                    ],
                  )),
            ],
          )),
    );
  }

  getDetail() {
    Get.find<HTTPS>().getData('/products/$uuid', {}).then((res) {
      if (res != null) {
        productsData.value = res['info'];
        boxInfo.value = res['box_info'];
        but.value = res['but'];

        if (res['box_info'].isNotEmpty) {
          indexItem.value = res['box_info'][0];
        }
        if (res['draw'].isNotEmpty) {
          drawData.value = res['draw'];
          timeList.value = res['draw']['time_list'];
          drawList.value = res['draw']['draw_list'];

          for (var i = 0; i < res['draw']['time_list'].length; i++) {
            if (res['draw']['time_list'][i]['is_set'] == 1) {
              timeIndex.value = i + 1;
            }
          }
        }
      }
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getDetail();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xffffffff),
        ),
        body: Column(
          children: [
            Expanded(
                child: ListView(
              children: [
                Obx(
                  () => Stack(
                    children: [
                      Image.network(
                          is_box == '1'
                              ? '${indexItem['thumb']}'
                              : '${productsData['thumb']}',
                          errorBuilder: (context, error, stackTrace) {
                        return SizedBox(
                          width: double.infinity,
                        );
                      }),
                      if (is_box == '1')
                        Positioned(
                          top: 20,
                          right: 15,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Container(
                                padding: EdgeInsets.symmetric(horizontal: 10),
                                alignment: Alignment.center,
                                height: 31,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(31),
                                  color: Color.fromARGB(128, 255, 255, 255),
                                ),
                                child: Obx(() => Text(
                                      '发行${indexItem['stock']}份',
                                      style: TextStyle(
                                          color: Color(0XFF000000),
                                          fontSize: 12,
                                          fontWeight: FontWeight.bold),
                                    )),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Container(
                                padding: EdgeInsets.symmetric(horizontal: 10),
                                alignment: Alignment.center,
                                height: 31,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(31),
                                  color: Color.fromARGB(128, 255, 255, 255),
                                ),
                                child: Obx(
                                  () => Text(
                                    '概率 ${indexItem['odds']} %',
                                    style: TextStyle(
                                        color: Color(0XFF000000),
                                        fontSize: 12,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                if (is_box == '1')
                  SizedBox(
                    height: 75,
                    child: Obx(() => ListView(
                          scrollDirection: Axis.horizontal,
                          children: [
                            SizedBox(
                              width: 15,
                            ),
                            for (var i = 0; i < boxInfo.length; i++)
                              Row(
                                children: [
                                  GestureDetector(
                                      onTap: () {
                                        _index.value = i;
                                        indexItem.value = boxInfo[i];
                                      },
                                      child: _index.value == i
                                          ? Container(
                                              padding: EdgeInsets.all(2),
                                              decoration: BoxDecoration(
                                                  borderRadius:
                                                      BorderRadius.circular(5),
                                                  border: Border.all(
                                                      color:
                                                          Color(0XFF542AFF))),
                                              child: ClipRRect(
                                                borderRadius:
                                                    BorderRadius.circular(5.0),
                                                child: Image.network(
                                                    '${boxInfo[i]['thumb']}',
                                                    width: 70,
                                                    fit: BoxFit.fill,
                                                    errorBuilder: (context,
                                                        error, stackTrace) {
                                                  return SizedBox(
                                                    width: 70,
                                                  );
                                                }),
                                              ))
                                          : Container(
                                              padding: EdgeInsets.all(2),
                                              decoration: BoxDecoration(
                                                  borderRadius:
                                                      BorderRadius.circular(5),
                                                  border: Border.all(
                                                      color:
                                                          Color(0XFFFFFFFF))),
                                              child: ColorFiltered(
                                                colorFilter:
                                                    const ColorFilter.matrix([
                                                  0.2126, 0.7152, 0.0722, 0,
                                                  0, // 红色通道权重
                                                  0.2126, 0.7152, 0.0722, 0,
                                                  0, // 绿色通道权重
                                                  0.2126, 0.7152, 0.0722, 0,
                                                  0, // 蓝色通道权重
                                                  0, 0, 0, 1, 0, // 透明度保持不变
                                                ]),
                                                child: ClipRRect(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          5.0),
                                                  child: Image.network(
                                                      '${boxInfo[i]['thumb']}',
                                                      width: 70,
                                                      fit: BoxFit.fill,
                                                      errorBuilder: (context,
                                                          error, stackTrace) {
                                                    return SizedBox(
                                                      width: 70,
                                                    );
                                                  }),
                                                ),
                                              ),
                                            )),
                                  SizedBox(
                                    width: 10,
                                  ),
                                ],
                              )
                          ],
                        )),
                  ),
             
                Padding(
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Obx(() => Text(
                              '限量${productsData['sell_num']}份，流通${productsData['release_num']}份',
                              style: TextStyle(
                                  fontSize: 14, color: Color(0XFF1F0584)),
                            )),
                        Obx(
                          () => Text(
                            '${productsData['title']}',
                            style: TextStyle(
                                fontSize: 18, color: Color(0XFF0F172B)),
                          ),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Row(
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  width: 70,
                                  child:  Text(
                                  '所在产链',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999)),
                                ),
                                )
                               ,
                                Obx(() => Text(
                                      '${productsData['link_name']}',
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(0XFF0F172B)),
                                    )),
                              ],
                            ),
                            SizedBox(
                              width: 100,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '发售时间',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999)),
                                ),
                                Obx(() => 
                                    productsData.isNotEmpty?
                                  Text(
                                      '${productsData['created_at'].split(' ')[0]}',
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(0XFF0F172B)),
                                    ):Text('')
                                    ),
                              ],
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Row(
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  width: 70,
                                  child: Text(
                                  '合约地址',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999)),
                                ),
                                )
                                ,
                                Obx(() => productsData.isNotEmpty &&
                                        productsData['link_address'].isNotEmpty
                                    ? Text(
                                        '${productsData['link_address'].substring(0, 3)}...${productsData['link_address'].substring(14, 18)}',
                                        style: TextStyle(
                                            fontSize: 14,
                                            color: Color(0XFF0F172B)),
                                      )
                                    : Text('...'))
                              ],
                            ),
                            SizedBox(
                              width: 100,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '支付方式',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999)),
                                ),
                                Obx(() => Text(
                                      '${productsData['pay_type']}',
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(0XFF0F172B)),
                                    )),
                              ],
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 25,
                        ),
                        Container(
                          decoration: BoxDecoration(
                              color: Color(0XFFF8F9FD),
                              borderRadius: BorderRadius.circular(8)),
                          child: Column(
                            children: [
                              Container(
                                height: 40,
                                decoration: BoxDecoration(
                                    color: Color(0XFF0F172B),
                                    borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(8),
                                        topRight: Radius.circular(8))),
                                child: Row(
                                  children: [
                                    SizedBox(
                                      width: 15,
                                    ),
                                    Text('抽签流程',
                                        style: TextStyle(
                                            fontSize: 16,
                                            fontWeight: FontWeight.bold,
                                            color: Color(0XFFFFFFFF))),
                                    Expanded(
                                      child: SizedBox(),
                                    ),
                                    Obx(() => Text('已有${drawData['count']}人参与',
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0XFFFFFFFF)))),
                                    SizedBox(
                                      width: 15,
                                    ),
                                  ],
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              Obx(() => Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    children: [
                                      for (var i = 0; i < timeList.length; i++)
                                        Text(
                                          '${timeList[i]['value']}',
                                          style: TextStyle(
                                              fontSize: 14,
                                              color:
                                                  timeList[i]['is_set'] == 1 ||
                                                          i < timeIndex.value
                                                      ? Color(0XFF999999)
                                                      : Color(0XFF0F172B)),
                                        ),
                                    ],
                                  )),
                              Obx(() => Padding(
                                  padding: EdgeInsets.only(
                                      top: 10, left: 25, right: 25, bottom: 5),
                                  child: timeList.isNotEmpty &&
                                          timeList[0]['is_set'] == 1
                                      ? Image.asset('static/web/chou1.png')
                                      : timeList.isNotEmpty &&
                                              timeList[1]['is_set'] == 1
                                          ? Image.asset('static/web/chou2.png')
                                          : timeList.isNotEmpty &&
                                                  timeList[2]['is_set'] == 1
                                              ? Image.asset(
                                                  'static/web/chou3.png')
                                              : timeList.isNotEmpty &&
                                                      timeList[3]['is_set'] == 1
                                                  ? Image.asset(
                                                      'static/web/chou4.png')
                                                  : Image.asset(
                                                      'static/web/chou.png'))),
                              Obx(() => Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      for (var i = 0; i < timeList.length; i++)
                                        Column(
                                          children: [
                                            Text(
                                              '${timeList[i]['time'].split(' ')[0]}',
                                              style: TextStyle(
                                                  fontSize: 11,
                                                  color: timeList[i]
                                                                  ['is_set'] ==
                                                              1 ||
                                                          i < timeIndex.value
                                                      ? Color(0XFF999999)
                                                      : Color(0XFF0F172B)),
                                            ),
                                            Text(
                                              '${timeList[i]['time'].split(' ')[1]}',
                                              style: TextStyle(
                                                  fontSize: 11,
                                                  color: timeList[i]
                                                                  ['is_set'] ==
                                                              1 ||
                                                          i < timeIndex.value
                                                      ? Color(0XFF999999)
                                                      : Color(0XFF0F172B)),
                                            ),
                                          ],
                                        ),
                                    ],
                                  )),
                              SizedBox(
                                height: 10,
                              ),
                            ],
                          ),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Container(
                          margin: EdgeInsets.only(bottom: 20, top: 10),
                          padding: EdgeInsets.symmetric(
                              horizontal: 15, vertical: 10),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: Color(0XFF0F172B)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    '我的抽签码',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFFFFFFFF)),
                                  ),
                                  Text(
                                    '解锁码越多，中签机率越大',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF999999)),
                                  ),
                                ],
                              ),
                              GestureDetector(
                                onTap: () => {getCodes()},
                                child: Container(
                                  alignment: Alignment.center,
                                  height: 27,
                                  width: 84,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(27),
                                      color: Color(0XFFFFFFFF)),
                                  child: Text(
                                    '点击查看',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF0F172B)),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        Text(
                          '藏品简介',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Obx(() => Column(
                              children: [
                                if (productsData.isNotEmpty)
                                  for (var item in productsData['images'])
                                    Image.network(item)
                              ],
                            )),
                        SizedBox(
                          height: 10,
                        ),
                        Text(
                          '购买须知',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          '数字藏品交易仅限于18周岁(不含)以上、60周岁(含)以下，并已完成实名认证的中国大陆用户购买。由于不同属性的数字艺术有不同的二次交易规则，请您仔细阅读并同意相关规则后再进行相应操作，以避免不必要的损失。 请注意，一旦数字藏品完成出售，不支持退货在购买之前，请仔细考虑您的购买决策。根据我国相关的法律要求，特定条件下的数字藏品的二次交易不支持任何形式的变相炒作。我们坚决反对数字藏品炒作、场外交易、欺诈，以及其他非法侵权行为。请您甄别上述涉嫌网络欺诈的行为并了解相关风险。',
                          style:
                              TextStyle(fontSize: 12, color: Color(0XFF999999)),
                        ),
                        SizedBox(
                          height: 15,
                        ),
                      ],
                    )),
              ],
            )),
            Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Obx(
                  () => Row(
                    children: [
                      timeIndex > 0 && timeIndex.value < 3
                          ? Expanded(
                              child: Container(
                              alignment: Alignment.center,
                              height: 40,
                              decoration: BoxDecoration(
                                color: Color.fromARGB(128, 0, 0, 0),
                              ),
                              child: Text(
                                '${timeList[2]['time']}结束',
                                style: TextStyle(color: Color(0XFFFFFFFF)),
                              ),
                            ))
                          : SizedBox()
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(vertical: 15),
                  decoration: BoxDecoration(
                    color: Color(0XFFFFFFFF),
                  ),
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 15),
                    height: 60,
                    decoration: BoxDecoration(
                        color: Color(0XFF0F172B),
                        borderRadius: BorderRadius.circular(60)),
                    child: Row(
                      children: [
                        SizedBox(
                          width: 25,
                        ),
                        Text(
                          '￥',
                          style:
                              TextStyle(fontSize: 16, color: Color(0XFFFFFFFF)),
                        ),
                        Text(
                          '99.00',
                          style:
                              TextStyle(fontSize: 24, color: Color(0XFFFFFFFF)),
                        ),
                        Expanded(child: SizedBox()),
                        GestureDetector(
                          onTap: () => {
                            if (timeList[0]['is_set'] == 1) {upraw()}else{
                              if(timeList[3]['is_set'] == 1){
Get.toNamed('/web_tabs/details/settlement?thumb=${productsData['thumb']}&is_box=$is_box&money_price=${productsData['money_price']/100}&uuid=${productsData['uuid']}&title=${productsData['title']}&draw=1')

                              }
                            }
                          },
                          child: Obx(() => Text(
                                timeList.isNotEmpty &&
                                        timeList[0]['is_set'] == 1
                                    ? '领取抽签码'
                                    : timeList.isNotEmpty &&
                                                timeList[2]['is_set'] == 1 ||
                                            timeList.isNotEmpty &&
                                                timeList[1]['is_set'] == 1
                                        ? '等待抽签'
                                        : '${but['but']}',
                                style: TextStyle(
                                    fontSize: 18, color: Color(0XFFFFFFFF)),
                              )),
                        ),
                        SizedBox(
                          width: 25,
                        ),
                      ],
                    ),
                  ),
                )
              ],
            )
          ],
        ));
  }
}
